package com.baoyang.controller;

import com.baoyang.entity.BaoyangUser;
import com.baoyang.entity.Container;
import com.baoyang.entity.vo.DriverUnloadCountRespVO;
import com.baoyang.entity.vo.PcDashboardRespVO;
import com.baoyang.entity.vo.ProductNameCountRespVO;
import com.baoyang.service.BaoyangUserService;
import com.baoyang.tool.api.R;
import com.baoyang.tool.utils.IpUtils;
import com.baoyang.tool.utils.JwtUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
@RequestMapping("/baoyang/user")
public class BaoyangUserController {
    @Resource
    private BaoyangUserService baoyangUserService;

    @Resource
    private JwtUtils jwtUtils;

    @GetMapping("/list")
    public List<BaoyangUser> list() {
        return baoyangUserService.listAll();
    }

    @PostMapping("/login")
    public R login(@RequestBody BaoyangUser user) {
        BaoyangUser dbUser = baoyangUserService.login(user);
        if (dbUser != null) {
            String token = jwtUtils.generateToken(dbUser.getUserName(), dbUser.getId());
            return R.data(token);
        } else {
            return R.fail("用户名或密码错误");
        }
    }

    @PostMapping("/register")
    public R register(@RequestBody BaoyangUser user) {
        boolean success = baoyangUserService.register(user);
        if (success) {
            return R.success("注册成功");
        } else {
            return R.fail("账号已存在");
        }
    }

    @PostMapping("/getinfo")
    public R getInfo(HttpServletRequest request) {
        System.out.println("进入getInfo方法");
        String userName = (String) request.getAttribute("userName"); // 这里要用"phone"

        if (userName == null) {
            return R.fail("token无效或已过期");
        }
        String ip = IpUtils.getClientIp(request);
        System.out.println("ip:" + ip);
        return R.data(baoyangUserService.getinfo(userName));
    }

    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null || token.isEmpty()) {
            return R.fail("未提供token");
        }
        jwtUtils.invalidateToken(token);
        return R.success("退出登录成功");
    }

    /**
     * 分页获取用户列表，排除id=1，支持nickName模糊查询和审核状态筛选
     */
    @GetMapping("/page")
    public R pageUserList(@RequestParam(defaultValue = "1") int pageNum,
                         @RequestParam(defaultValue = "10") int pageSize,
                         @RequestParam(required = false) String nickName,
                         @RequestParam(required = false) Integer auditStatus) {
        Page<BaoyangUser> page = new Page<>(pageNum, pageSize);
        IPage<BaoyangUser> userPage = baoyangUserService.pageUserList(page, nickName, auditStatus);
        return R.data(userPage);
    }

    /**
     * 审核通过
     */
    @PostMapping("/audit")
    public R audit(@RequestBody BaoyangUser user) {
        return R.status(baoyangUserService.audit(user));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody BaoyangUser user) {
        return R.status(baoyangUserService.update(user));
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {
        return R.status(baoyangUserService.removeById(id));
    }

    /**
     * 司机卸货统计
     */
    @GetMapping("/driverUnloadCountPageList")
    public R driverUnloadCount(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam(required = false) String startTime,
                               @RequestParam(required = false) String endTime,
                               @RequestParam(required = false) String carNumber,
                               @RequestParam(required = false)String nickName) {
        IPage<DriverUnloadCountRespVO> result = baoyangUserService.driverUnloadCount(pageNum, pageSize, startTime, endTime, carNumber, nickName);
        return R.data(result);
    }

    /**
     * pc端数据看板最上方统计
     */
    @GetMapping("/dashboard/count")
    public R dashboardCount() {
        PcDashboardRespVO result = baoyangUserService.dashboardCount();
        return R.data(result);
    }

    /**
     * 获取所有司机列表
     */
    @GetMapping("/driverList")
    public R driverList() {
        List<BaoyangUser> result = baoyangUserService.driverList();
        return R.data(result);
    }

    /**
     * 注册开关
     */
    @GetMapping("/registerSwitch")
    public R registerSwitch() {
        return R.data(1);
    }

}